package com.xone.xapp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.xone.refresh.library.PullToRefreshListView;

public class SubscribeMatcherListAcitvity extends BasicListActivity<ProductListHolder> {
	protected ProductAdapter productAdapter;
	protected Map<String, String> params = new HashMap<String, String>();
	protected PullToRefreshListView listView;
	protected String dateCreated = null;
	@Override
	public int getContentView() {
		return R.layout.subscribe_matcher_list;
	}
	@Override
	public String getHeaderTitle() {
		return "产品订阅列表";
	}
	/**
	 * 列表视图
	 */
	@Override
	public PullToRefreshListView getListView() {
		return listView;
	}
	/**
	 * 列表适配器
	 */
	@Override
	public BasicListAdapter<ProductListHolder> getListAdapter(Context context,
			List<JSONObject> result) {
		return new ProductAdapter(context, result) {
			@Override
			public int getTopLastIndex() {
				return -1;
			}
		};
	}
	/**
	 * 列表的请求地址
	 */
	@Override
	public String getRequestURL() {
		return "/product/listJsonSubscribeItems.html";
	}
	/**
	 * 列表的请求参数
	 */
	@Override
	public List<NameValuePair> getRequestParams() {
		List<NameValuePair> l = new ArrayList<NameValuePair>();
		for (Map.Entry<String, String> m : params.entrySet()) {
			l.add(new BasicNameValuePair(m.getKey(), m.getValue()));
		}
		return l;
	}
	/**
	 * 返回的请求中的json根列表名称
	 */
	@Override
	public String getJsonRootName() {
		return "list";
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (!ContextUtils.isOnline(this)) {
			return;
		}
		listView = (PullToRefreshListView) findViewById(R.id.subscribe_matcher_list);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	protected void onResume() {
		Bundle bundle = this.getIntent().getExtras();
		String name = null;
		if (null != bundle) {
			name = bundle.getString("name");
			if (StringUtils.isNotBlank(name)) {
				params.put("product.productName", name);
			} 
			String saleType = bundle.getString("saleType");
			if (StringUtils.isNotBlank(saleType)) {
				params.put("product.saleType", saleType);
			}
			String credit = bundle.getString("credit");
			if (StringUtils.isNotBlank(credit)) {
				params.put("credit", credit);
			}
			String area = bundle.getString("area");
			if (StringUtils.isNotBlank(area)) {
				params.put("product.productLocation", area);
			}
			dateCreated = params.put("product.gtDateCreated", bundle.getString("dateCreated"));
			Log.i(TAG, "收到参数：" + name + "," + saleType + "," + credit + "," + area);
		}
		super.onResume();
		if (StringUtils.isBlank(name)) {
			Toast.makeText(getContext(), "缺少基本的查询元素", Toast.LENGTH_SHORT).show();
			this.finish();
		}
		showProgress();
		new InitDataTask(this, listView).execute(getRequestParams());
	}
}
